import DeviceInfo from 'react-native-device-info'
import {DeviceEventEmitter} from 'react-native';
import AppEvent from './AppEvent';
class RequestUtil {


    static get(url,params){
        if (params) {
            let paramsArray = [];
            //encodeURIComponent
            Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
            if (url.search(/\?/) === -1) {
                url += '?' + paramsArray.join('&')
            } else {
                url += '&' + paramsArray.join('&')
            }
        }
        console.log('get请求地址',url)
        return  this.sendRequest('GET',url,null)
    }

    /**
     * 基于 fetch 封装的 POST请求  FormData 表单数据
     * @param url
     * @param formData
     * @param headers
     * @returns {Promise}
     */
    static post = ( url, params,isFormdata ) => {

        return this.sendRequest('POST',url,params,isFormdata)
    }
    /**
     * 基于 fetch 封装的请求  FormData 表单数据
     * @param method方法
     * @param url 请求接口
     * @param param参数
     * @param filter 过滤返回结果  默认过滤
     * @returns {Promise}
     */
    static sendRequest = ( method,url, params,isFormdata ) => {

        let headers = {
            'Content-Type': 'application/json',
            'client-version': '1.0',
            'language': 'vi',
            'Accept-Language': 'vi',
        }
        let body = params
        if(method === 'POST'){
            if(isFormdata){
                //如果是formdata
                console.log('参数是formdata')
                headers['Content-Type'] = 'multipart/form-data';

                var formData = new FormData();
                for (var k in params) {
                    formData.append(k, params[k]);
                }
                body = formData
            }else{
                headers['Content-Type'] = 'application/x-www-form-urlencoded';
                let tempBody = new URLSearchParams()
                for(var k in params){
                    tempBody.append(k,params[k])
                }
                body = tempBody.toString()
                // body = JSON.stringify(params)
            }
        }


        if(global.token){
            headers['user-token'] = global.token
        }

        let isOk;

        return new Promise((resolve, reject) => {
            fetch(url, {
                method: method,
                headers: headers,
                body: body,
            })
                .then((response) => {
                    if (response.ok) {
                        isOk = true;
                    } else {
                        isOk = false;
                    }
                    return response.json();
                })
                .then((json) => {

                    if (isOk ) {
                        if(json.code === 0){
                            resolve(json)
                        }else{
                            if(json.code === -3){
                                DeviceEventEmitter.emit(AppEvent.login_expire);
                            }
                            console.log('错误',json)
                            reject(json)
                        }

                    } else {
                        let err= {code:-111,msg:'Disconnect'}
                        reject(err)
                    }
                })
                .catch((err) => {
                    console.log(err)
                    let error= {code:-111,msg:'Disconnect'}
                    reject(error);
                })
        })
    }


}


export default RequestUtil
